<?php
include '_init_.php';
	$orderManager = Order_Manager::getOrderManager($_REQUEST['id']);
if($orderManager == null)
	exit("Not exist");
if($orderManager->getStatus() != Order_Manager::$STATUS_SAVED)
	exit("Not allowed");
$orderManager->init();

$orderInfo = $orderManager->getInfo();
$act = isset($_REQUEST['act'])?$_REQUEST['act']:'';
$dtGift = new DataTable(constant('DB_TABLE_GIFT'));
if($act == 'save')
{
	$gift_list = array();
	//construct zoom list data
	$is_gift = isset($_REQUEST['is_gift'])?'Y':'N';
	foreach($_REQUEST as $reKey=>$reValue)
	{
		if(strpos($reKey,'gift_id_') === false)
			continue;
		$gift_id = substr($reKey,8);
		$gift_data = $dtGift->get($gift_id);
		$number = intval($_REQUEST['gift_number_'.$gift_id]);
		$gift_list[] = array('gift_id'=>$gift_id,'gift_name'=>$gift_data['name'],'number'=>$number,'is_gift'=>$is_gift);
	}
	$updateOrderData = array('gift_list'=>$gift_list);

	$orderManager->setInfo($updateOrderData);
	$orderManager->persistOrderInfo();
	AppMain::refreshParentFrame();
}




$giftList = $dtGift->get_list('','name');

$order_gift_list = $orderInfo['gift_list'];
$order_gift_list = Util::convertArrayToMap($order_gift_list,'gift_id');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
</head>

<body>
<form action="cosih_add.php">
<input type="hidden" name="act" value="save"/>
<input type="hidden" name="id" value="<?php echo $orderManager->getId();?>"/>


<div class="mainf-8">
    <ul>
    <li class="maina-1k">周邊相關管理</li>
      <li class="mainf-1m"><img src="images/line5.gif" width="676" height="7" align="top" /></li>
      <li class="maina-1c2">
<input name="is_gift" type="checkbox" class="article-adminb-chbox" />
贈品
    </li>
    	<?php foreach($giftList as $tmpData):?>
    	<?php
    		$checked = '';
    		$number = '';
    		if(array_key_exists($tmpData['id'],$order_gift_list))
    		{
    			$checked = 'checked="checked"';
    			$number = $order_gift_list[$tmpData['id']]['number'];
    		}
    	?>
    	<li class="maina-1a">
        <input type="checkbox"  <?php echo $checked;?> name="gift_id_<?php echo $tmpData['id'];?>" />&nbsp;<?php echo $tmpData['name'];?> 
        <input name="gift_number_<?php echo $tmpData['id'];?>" type="text"  class="maine-keying" value=" <?php echo $number;?>"/>
        </li>
    	<?php endforeach;?> 
  </ul>
</div>

<div class="mainf-7">
<ul>
<li class="article-1g"><input name="" type="button" class="article-1a_btn" value="儲存" onclick="document.forms[0].submit();return false;"/></li>
</ul>
</div>
</form>
</body>
</html>
